# 05 - Advanced RAG Flows/local_embedding.py
import requests
from typing import List
from langchain_core.embeddings import Embeddings


class LocalFastAPIEmbeddings(Embeddings):
    def __init__(self, api_url: str = "http://localhost:8080/embeddings", model: str = "xlm-roberta-large"):
        self.api_url = api_url
        self.model = model

    def embed_documents(self, texts: List[str]) -> List[List[float]]:
        payload = {
            "input": texts,
            "model": self.model
        }
        response = requests.post(self.api_url, json=payload)
        if response.status_code != 200:
            raise Exception(f"API request failed with status {response.status_code}: {response.text}")
        return [item["embedding"] for item in response.json()["data"]]

    def embed_query(self, text: str) -> List[float]:
        return self.embed_documents([text])[0]
